package com.xiaobaibai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaobaibai.dao.UserAddressDao;
import com.xiaobaibai.entity.UserAddress;
import com.xiaobaibai.service.IUserAddressService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaobaibai
 * @since 2020-10-16
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressDao, UserAddress> implements IUserAddressService {

    @Override
    @Cacheable(value = {"address"},key = "#root.methodName+'-'+#userId")
    public List<UserAddress> getAddressByUserId(Integer userId) {
        QueryWrapper<UserAddress> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("id","user_id","user_name","user_phone","user_address_detail","flag");
        queryWrapper.eq("user_id",userId);
        return this.list(queryWrapper);
    }

    @Override
    //清除目标用户的地址缓存
    @CacheEvict(value = {"address"},key = "'getAddressByUserId-'+#userAddress.userId")
    public boolean addUserAddress(UserAddress userAddress) {
        return this.save(userAddress);
    }

    @Override
    @CacheEvict(value = {"address"},key = "'getAddressByUserId-'+#userId")
    public void updateAddressDefault(UpdateWrapper<UserAddress> updateWrapper,Integer userId) {
        this.update(updateWrapper);
    }

}
